package com.polaris.live.im.sfs.processor.impl.system

import com.polaris.live.common.cacha.CacheUtil
import com.polaris.live.im.db.database.AppDatabase
import com.polaris.live.im.sfs.core.SmartFoxManager
import com.polaris.live.im.sfs.core.bean.DataArgument
import com.polaris.live.im.sfs.processor.MessageProcessSignal
import com.polaris.live.im.sfs.processor.impl.SingleMessageMessageProcessor
import kotlinx.coroutines.launch
import java.util.concurrent.TimeUnit

/**
 * ExcludeMessageProcess
 *
 * @author Created by lucas on 2023/10/21 10:17
 */
class ExcludeMessageProcessor : AbstractExcludeMessageProcessor() {

    override fun order(): Int = Int.MAX_VALUE - 1

    override fun shouldProcess(type: Int, command: String): Boolean {
        return command in commands
    }

    override suspend fun process(type: Int, command: String, argument: DataArgument): MessageProcessSignal {
        return exclude(type, argument)
    }

    override suspend fun handle(type: Int, command: String, argument: DataArgument) {
        //ignore
    }

    companion object {

        val commands = SingleMessageMessageProcessor.commands

        fun clearRedundancy() {
            SmartFoxManager.messageScope.launch {
                AppDatabase.instance.messageExcludeDao()
                    .clearRedundancy(CacheUtil.getServerTime() - TimeUnit.DAYS.toMillis(15))
            }
        }
    }
}